

import bpy
import numpy as np
import copy
import 全局控制.Node.节点和插座基类.bl_UpdateNode as 更新
import 全局控制.Node.节点和插座基类.bl_NT_Update as Tree更新
import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
from bpy.types import Operator
import inspect
import pdb

def f_update(self, context):
	if self.m_物体 != None :
		self.m_物体.display.show_shadows = False
		self.m_物体.display_type = 'WIRE'
		self.f_pushUpdate()

		self.outputs['父物体'].f_setData(self.m_物体)
		#super(C_SceneNode_物体域, self).f_pushUpdate()
	

def f_update域维度(self, context):
	self.f_pushUpdate()
	#pdb
	Tree更新.f_updateNode({self.name:self})
	#self.f_update(是否向下传递=True)

class C_ops_更新域矩阵数据(bpy.types.Operator):
	bl_idname = 'node.update_node_domain_matrix'
	bl_label = '更新域矩阵数据'
	#bl_options = {'REGISTER', 'UNDO'}
	
	m_节点树ID名称 = bpy.props.StringProperty(name='节点树ID名称', default='')
	m_节点ID名称 = bpy.props.StringProperty(name='节点ID名称', default='')
	
	def execute(self, context):
		node_group = bpy.data.node_groups[self.m_节点树ID名称]
		node = node_group.nodes[self.m_节点ID名称]
		
		if node :
			f_update域维度(node, context)
		return {'FINISHED'}
		

class C_SceneNode_物体域(基类.C_SN_基类): 
	bl_dname = 'bl_node_物体域'
	bl_label = '物体域'
	
	
	
	m_物体 = bpy.props.PointerProperty(name='物体', type=bpy.types.Object, update=f_update)
	
	def init(self, context):
		super(C_SceneNode_物体域, self).f_init(self)
		self.outputs.new('C_NodeSocket_N矩阵_f', name='输出坐标数量')
		self.outputs.new('C_NodeSocket_3i', name='输出维度')
		self.outputs.new('C_NodeSocket_3f', name='单元尺寸')
		
		self.outputs.new('C_NodeSocket_3f', name='中心偏移')
		self.outputs.new('C_NodeSocket_3f', name='边长')
		self.outputs.new('C_NodeSocket_3f', name='Lower')
		self.outputs.new('C_NodeSocket_3f', name='Upper')
		
		self.outputs.new('C_NodeSocket_物体', name='父物体')

		self.outputs['输出维度'].f_setData((1,1,1))
		


	def draw_buttons(self, context, layout):
		layout.prop(self, "m_物体")

		opera = layout.operator("node.update_node_domain_matrix")
		opera.m_节点树ID名称 = self.id_data.name
		opera.m_节点ID名称 = self.name

	#def update(self):
	#	print('update domain node')
	def f_is自动Update(self):
		return False

	def f_update(self, 是否向下传递=False, input插座=None):
		if self.m_物体 == None : return
		
		元素比例插座 = self.outputs['单元尺寸']

		插座 = self.outputs['输出坐标数量']
		插座.f_clearData()

		#包围盒
		跨度 = [1.0,1.0,1.0]
		x = np.linspace(0, 1, len(self.m_物体.data.vertices))
		y = np.linspace(0, 1, len(self.m_物体.data.vertices))
		z = np.linspace(0, 1, len(self.m_物体.data.vertices))
		for i in range(len(self.m_物体.data.vertices)):
			x[i] = self.m_物体.data.vertices[i].co.x
			y[i] = self.m_物体.data.vertices[i].co.y
			z[i] = self.m_物体.data.vertices[i].co.z

		Lower = [x.min(), y.min(), z.min()]
		Upper = [x.max(), y.max(), z.max()]
		self.outputs['Lower'].f_setData(Lower)
		self.outputs['Upper'].f_setData(Upper)

		
		offset = [0,0,0]
		offset[0] = x.min()
		offset[1] = y.min()
		offset[2] = z.min()

		#正方向+负方向=总长度
		跨度[0] = abs(Upper[0]) + abs(Lower[0])
		跨度[1] = abs(Upper[1]) + abs(Lower[1])
		跨度[2] = abs(Upper[2]) + abs(Lower[2])
		self.outputs['边长'].f_setData(跨度)
		
		域细分 = self.outputs['输出维度'].f_getData()
		跨度[0] /= float(域细分[0]) #4
		跨度[1] /= float(域细分[1]) #3
		跨度[2] /= float(域细分[2]) #1
		
		offset[0] += self.m_物体.location.x + 跨度[0]*0.5
		offset[1] += self.m_物体.location.y + 跨度[1]*0.5
		offset[2] += self.m_物体.location.z + 跨度[2]*0.5
		self.outputs['中心偏移'].f_setData(offset)
		

		
		元素比例插座.f_setData((跨度[0], 跨度[1], 跨度[2]))
		
		for z in range(域细分[2]):
			#my = []
			for y in range(域细分[1]):
				#mx = []
				for x in range(域细分[0]):
					coord = [ x*跨度[0] + offset[0], y*跨度[1] + offset[1], z*跨度[2] + offset[2] ]
					插座.f_addData((int(x), int(y), int(z)), (coord[0], coord[1], coord[2]))
		#pdb.set_trace()
		super(C_SceneNode_物体域, self).f_update(是否向下传递, input插座)
		#self.f_标记为已更新()

		#print('self.f_is以更新() = ', self.f_is以更新())


	


class C_NODE_物体域(bpy.types.Operator):
	bl_idname = 'node.global_node_object_domain_add'
	bl_label = 'Add 物体域'
	bl_options = {'REGISTER', 'UNDO'}

	def execute(operator, context):
		operator.use_transform = True
		bpy.ops.node.add_node(type='C_SceneNode_物体域', use_transform=True)
		return {'FINISHED'}




classes = (
	C_ops_更新域矩阵数据,

	C_SceneNode_物体域,
	C_NODE_物体域,
)




def register():
	for c in classes:
		bpy.utils.register_class(c)
		

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	

